﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Web;
using System.Web.Mvc;
using DataMongo;
using DataMongo.Entities;

namespace MVCMongo.Controllers
{
    public class PeopleSearchController : Controller
    {
        //
        // GET: /PeopleSearch/

        public ActionResult Search()
        {
            return View();
        }

        public ActionResult SearchParameters()
        {
            var search = new SearchParameter();

            return PartialView("_PartialSearchParameter", search);
        }

        [HttpPost]
        public ActionResult SearchParameters(SearchParameter modelParameter)
        {
            var ageRange = modelParameter.AgeFrom.Split('-');
            modelParameter.AgeFrom = ageRange[0];
            modelParameter.AgeTo = ageRange[1];
            var userConnection = new RepositoryBaseMongo("Users");
            var userdetailConnection = new RepositoryBaseMongo("UserDetails");
            List<Users> allusers=new List<Users>();
            if (modelParameter.CountryName != null && modelParameter.CityName != null)
            {
                allusers = (from f in userConnection.GetAll<Users>()
                            where (((DateTime.UtcNow - f.BirthDay).TotalDays / 365) >= Convert.ToInt32(modelParameter.AgeFrom) &&
                            ((DateTime.UtcNow - f.BirthDay).TotalDays / 365) <= Convert.ToInt32(modelParameter.AgeTo) && f.Gender == modelParameter.Gender)
                            join userdetailss in userdetailConnection.GetAll<UserDetails>() on f.UserId equals userdetailss.UserId
                            where userdetailss.Country.ToLower() == modelParameter.CountryName.ToLower() && userdetailss.City.ToLower() == modelParameter.CityName.ToLower()
                            select f).ToList();
            }
            else if (modelParameter.CountryName == null && modelParameter.CityName != null)
            {
                allusers = (from f in userConnection.GetAll<Users>()
                            where (((DateTime.UtcNow - f.BirthDay).TotalDays / 365) >= Convert.ToInt32(modelParameter.AgeFrom) &&
                            ((DateTime.UtcNow - f.BirthDay).TotalDays / 365) <= Convert.ToInt32(modelParameter.AgeTo) && f.Gender == modelParameter.Gender)
                            join userdetailss in userdetailConnection.GetAll<UserDetails>() on f.UserId equals userdetailss.UserId
                            where userdetailss.Country.ToLower() == modelParameter.CountryName.ToLower()
                            select f).ToList();
            }
            else if (modelParameter.CountryName != null && modelParameter.CityName == null)
            {
                allusers = (from f in userConnection.GetAll<Users>()
                            where (((DateTime.UtcNow - f.BirthDay).TotalDays / 365) >= Convert.ToInt32(modelParameter.AgeFrom) &&
                            ((DateTime.UtcNow - f.BirthDay).TotalDays / 365) <= Convert.ToInt32(modelParameter.AgeTo) && f.Gender == modelParameter.Gender)
                            join userdetailss in userdetailConnection.GetAll<UserDetails>() on f.UserId equals userdetailss.UserId
                            where userdetailss.City.ToLower() == modelParameter.CityName.ToLower()
                            select f).ToList();
            }
            else
            {
                allusers = (from f in userConnection.GetAll<Users>()
                            where (((DateTime.UtcNow - f.BirthDay).TotalDays / 365) >= Convert.ToInt32(modelParameter.AgeFrom) &&
                            ((DateTime.UtcNow - f.BirthDay).TotalDays / 365) <= Convert.ToInt32(modelParameter.AgeTo) && f.Gender == modelParameter.Gender)
                            select f).ToList();
            }



            return PartialView("_PartialUsersSearchList", allusers);
        }
        public ActionResult SearchResult()
        {
            var connection = new RepositoryBaseMongo("Users");
            var allUserUser = connection.GetAll<Users>();

            return PartialView("_PartialUsersSearchList", allUserUser);
        }

        
    }
}
